
from rest_framework import generics
from rest_framework.filters import OrderingFilter, SearchFilter
from django_filters.rest_framework import DjangoFilterBackend

from . import models, serializers
from .filters import limitCourseFilter, CourseAdvancedFilter
from .coursePaginations import CoursePageNumberPagination

class CategoryListAPIView(generics.ListAPIView):
    queryset = models.CourseCategory.objects.filter(is_delete=False, is_show=True)
    serializer_class = serializers.CategoryModelSerializer


class FreeCourseListAPIView(generics.ListAPIView):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True)
    serializer_class = serializers.CourseFreeModelSerializer

    # 配置过滤类
    filter_backends = [OrderingFilter, limitCourseFilter, SearchFilter, DjangoFilterBackend]

    # 配置排序过滤关键字
    ordering_fields = ['price', 'id', 'students']
    # 配置搜索过滤关键字
    search_fields = ['name']
    # 配置分页类
    # 需要在dev中的drf配置中配置PAGE_SIZE，为每页条数
    # 并且不能做到自定义每页条数
    # pagination_class = PageNumberPagination
    # 自定义的分页类，修改原类中的一些方法
    pagination_class = CoursePageNumberPagination
    # 配置自定义的高级过滤器，还需要在
    filter_class = CourseAdvancedFilter


class FreeCourseDetailRetrieveAPIView(generics.RetrieveAPIView):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class = serializers.CourseFreeModelSerializer




